<?php

namespace App\Admin\Controller\Cms\Article;

class Reply extends \App\Admin\Controller\Authed {

    public function index() {

        $this->article_id = $article_id = $this->request->get('article_id');

        $filter_article = urldecode(trim($this->request->get('filter_article')));
        $filter_begin = trim($this->request->get('filter_begin', ''));
        $filter_end = trim($this->request->get('filter_end', ''));

        if ($article_id && !$filter_article) {
            $this->filter_article = Cms()->Article()->getc($article_id, 'title');
        } else {
            if ($filter_article) {
                $article_ids = Cms()->Article()->searchIds($filter_article);
                if ($article_ids) {
                    $filter['article_id'] = array('in' => $article_ids);
                } else {
                    $filter['article_id'] = 0;
                }
            }
        }

        if ($filter_begin && $filter_end) {
            $filter['add_time'] = array('between' => $filter_begin, 'and' => $filter_end . ' 23:59:59');
        }

        if ($article_id) {
            $filter['article_id'] = $article_id;
        }

        $count = Cms()->Article()->Reply()->countAll($filter);

        /* 分页 */
        $pager = $this->_createPager($count);
        $length = $pager['length'];
        $start = $pager['offset'];
        $this->assign('pager', $pager);

        $rows = Cms()->Article()->Reply()->getAll($start, $length, $filter);
        foreach ($rows as &$row) {
            $row['uid'] && $row['nickname'] = QF()->User()->getc($row['uid'], 'nickname');
            $row['content'] = htmlspecialchars_decode($row['content']);
            $row['article_title'] = Cms()->Article()->getc($row['article_id'], 'title');
        }
        unset($row);

        $this->assign('rows', $rows);
    }

    public function remove() {

        $id = intval($this->request->get('id'));

        $result = Cms()->Article()->Reply()->remove($id);

        if ($result) {
            succ();
        } else {
            fail();
        }
    }

}
